<template>
  <el-drawer v-model="drawer" :direction="direction" @close="closeclick">
    <template #title>
      <h4>{{ title }}</h4>
    </template>
    <template #default>
      <el-form
        ref="ruleFormRef"
        :model="tableData"
        status-icon
        :rules="rules"
        label-width="120px"
        class="demo-ruleForm"
      >
        <el-form-item label="设备名称" prop="deviceName">
          <el-input
            v-model="tableData.deviceName"
            clearable
            placeholder="请输入设备名称"
          />
        </el-form-item>
        <el-form-item label="设备IP地址" prop="deviceAddress">
          <el-input
            v-model="tableData.deviceAddress"
            clearable
            placeholder="请输入IP地址"
          />
        </el-form-item>
        <el-form-item label="设备状态">
          <div>{{ tableData.deviceStatus }}</div>
        </el-form-item>
        <el-form-item label="设备电量">
          <div>{{ tableData.deviceBattery }}</div>
        </el-form-item>
        <el-form-item label="开始隔间">
          <div>{{ tableData.deviceStartPan }}</div>
        </el-form-item>
        <el-form-item label="最后更新时间">
          <div>{{ tableData.lastUpdateTime }}</div>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            :loading="loading"
            @click="submitForm(ruleFormRef, tableData)"
            >提交</el-button
          >
          <el-button @click="resetForm(ruleFormRef)">取消</el-button>
        </el-form-item>
      </el-form>
    </template>
    <template #footer> </template>
  </el-drawer>
</template>

<script  setup>
import { ref, defineProps, watch, defineEmits, reactive } from 'vue'
import { deviceSaveupdate } from '@/api/equipment'
import { ElMessage } from 'element-plus'
import { rulesNumber, rulesIP } from '@/utils/rules'
const props = defineProps({
  title: {
    type: String
  },
  showDrawer: {
    type: Boolean,
    // 必传
    required: true
  },
  amendEquipment: {
    type: Object
  }
})
// 定义一个变量 接收父组件传递来的值
const tableData = reactive({})
// 侦听传递过来的数据
watch(
  () => props.amendEquipment,
  (value) => {
    Object.assign(tableData, value)
  },
  {
    immediate: true,
    deep: true
  }
)
// 表单校验
const ruleFormRef = ref()

const rules = reactive({
  deviceName: [
    { required: true, message: '设备名称不能为空', trigger: 'blur' },
    { validator: rulesNumber, trigger: 'blur' }
  ],
  deviceAddress: [
    { required: true, message: '设备IP不能为空', trigger: 'blur' },
    { validator: rulesIP, trigger: 'blur' }
  ]
})
// 提交
const loading = ref(false)
const submitForm = (formEl, tableData) => {
  // console.log(tableData)
  const { deviceId, deviceName, deviceAddress } = tableData
  formEl.validate((valid) => {
    if (valid) {
      loading.value = true
      setTimeout(() => {
        loading.value = false
        // 验证通过调用修改接口
        deviceSaveupdate({
          deviceId: deviceId,
          deviceName: deviceName,
          deviceAddress: deviceAddress
        })
          .then((res) => {
            ElMessage({
              message: res.message,
              type: 'success'
            })
            emit('clickShow', false, res.code)
          })
          .catch((err) => {
            ElMessage.error(err.message)
            emit('clickShow', false)
          })
      }, 500)
    } else {
      ElMessage.error('失败')
      return false
    }
  })
}
// 取消提交
const resetForm = (formEl) => {
  if (!formEl) return
  formEl.resetFields()
  drawer.value = false
  emit('clickShow', false)
}

// 抽屉的隐藏显示
const emit = defineEmits(['clickShow'])
const drawer = ref(false)
watch(
  () => props.showDrawer,
  (value) => {
    drawer.value = value
    // console.log(drawer.value)
  }
)

// 抽屉方向  rtl / ltr / ttb / btt
const direction = ref('rtl')
// 监听抽屉关闭
const closeclick = () => {
  emit('clickShow', false)
  ruleFormRef.value.resetFields()
  Object.keys(tableData).forEach((key) => {
    tableData[key] = ''
  })
}
</script>
